package com.aaa.controller;

import com.aaa.entity.Back_User;
import com.aaa.entity.Foul;
import com.aaa.entity.Role;
import com.aaa.service.impl.BackUserImpl;
import com.aaa.service.impl.RoleServiceImpl;
import com.aaa.utils.upload;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: BackUserController
 * @Description: TODO
 * @Author: 86139
 * @Date: 2021/9/7 13:37
 * @Version:1.0
 **/
@RestController
@RequestMapping("backuser")
public class BackUserController {
    @Resource
    BackUserImpl backUser;
    @Resource
    RoleServiceImpl roleService;
    @RequestMapping("finAll")
    public PageInfo finAll(Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Back_User> list = backUser.list();
        PageInfo<Back_User> rolePageInfo = new PageInfo<Back_User>(list);
        return rolePageInfo;
    }
    @RequestMapping("allRname")
    public List<Role> allRname(){
        return roleService.list();
    }
    @RequestMapping("add")
    public boolean add(Back_User back_user){
        QueryWrapper<Back_User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("username",back_user.getUsername());
        List<Map<String, Object>> list1 = backUser.listMaps(objectQueryWrapper);
        if(list1.size()>0){
            return false;
        }
        back_user.setPassword("123");
        back_user.setPassword("$2a$10$K62SbdAxtt8FuKCbfk.NtOA12od0A3.pkX6bBOy3eew0OQdKXS/V2");
        back_user.setPhoto("20210908092125.jpg");
        back_user.setState(0);
        return backUser.save(back_user);
    }
    @RequestMapping("update")
    public boolean update(Back_User back_user){
        QueryWrapper<Back_User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("username",back_user.getUsername());
        List<Map<String,Object>> list = backUser.listMaps(objectQueryWrapper);
        System.out.println(list);
        if(list.size()>0){
            QueryWrapper<Back_User> wrapper = new QueryWrapper<>();
            objectQueryWrapper.eq("uid",back_user.getUid());
            Map<String,Object> list1 = backUser.listMaps(wrapper).get(0);
            if(list1.get("username").equals(back_user.getUsername())){
                return backUser.updateById(back_user);
            }
            return false;
        }else {
            return backUser.updateById(back_user);
        }
    }
    @RequestMapping("state")
    public boolean state(Back_User back_user){
        return backUser.updateById(back_user);
    }
}
